﻿using ES.Migrate;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FileMigrateWorker_v3._0
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = true;
            
        }

        private void btnSrcPath_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDia = new OpenFileDialog();
            var dia = fileDia.ShowDialog();
            if (dia == DialogResult.OK)
            {
                txtSrc.Text = fileDia.FileName;
            }
        }

        private void btnToPath_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbDia = new FolderBrowserDialog();
            var dia = fbDia.ShowDialog();
            if (dia == DialogResult.OK)
            {
                string fileName = Path.GetFileName(txtSrc.Text);
                txtTo.Text = Path.Combine(fbDia.SelectedPath, fileName);
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            this.btnPause.Enabled = false;
            this.btnContince.Enabled = false;
            this.btnRedo.Enabled = false;
            this.btnCancel.Enabled = false;
        }

        private ES.Migrate.MigrateWorker2<string> worker = null;

        private delegate void UpProg(int value);
        private delegate void UpMsg(string msg);
        private delegate void UpEnabled(Control ctl, bool bl);

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (!File.Exists(txtSrc.Text))
            {
                MessageBox.Show("源文件路径的文件不存在！", "提示", MessageBoxButtons.OK);
                return;
            }

            lblMsg.Text = string.Empty;

            worker = new MigrateWorker2<string>(string.Empty,txtSrc.Text, txtTo.Text);

            this.fileProgressBar.Maximum = 100;

            worker.OnStart += (sdr, res) =>
            {
                Console.WriteLine("start");
            };

            worker.OnUpdateProgress += (sdr,res) =>
            {
                decimal prog = 0;
                if (res.Rate == 0.990m)
                {
                    prog = res.Rate * 100;
                }
                else
                {
                    int val = (int)Math.Floor(res.Rate / 0.125m);
                    prog = (val * 0.125m) * 100;
                }

                Console.WriteLine("OnUpdateProgress：" + prog + "%");
                this.fileProgressBar.Invoke(new UpProg(upProg), Convert.ToInt32(prog));

            };

            worker.OnError += (sdr, res) =>
            {
                Console.WriteLine("OnError：" + res.Exception.ToString());
                lblMsg.Invoke(new UpMsg(upMsg), res.Exception.Message);
            };

            worker.OnMd5Checking += (sdr, res) =>
            {
                Console.WriteLine("OnMd5Checking");
            };

            worker.OnCompleted += (sdr,res) =>
            {
                Console.WriteLine("OnCompleted：" + res.CheckResult?.ToString());

                lblMsg.Invoke(new UpMsg(upMsg), res.CheckResult?.ToString());

                this.fileProgressBar.Invoke(new UpProg(upProg), 100);
                this.btnStart.Invoke(new UpEnabled(upEnabled), this.btnStart, true);
                this.btnPause.Invoke(new UpEnabled(upEnabled), this.btnPause, false);
            };

            this.btnStart.Enabled = false;
            this.btnPause.Enabled = true;

            worker.Start();
        }

        

        public void upProg(int value)
        {
            this.fileProgressBar.Value = value;
        }

        public void upMsg(string msg)
        {
            this.lblMsg.Text = msg;
        }

        
        public void upEnabled(Control ctl,bool bl)
        {
            ctl.Enabled = bl;
        }


        private void btnPause_Click(object sender, EventArgs e)
        {
            this.btnPause.Enabled = false;
            this.btnContince.Enabled = true;
            this.btnRedo.Enabled = true;
            this.btnCancel.Enabled = true;
            worker.PauseAsync();
        }

        private void btnContince_Click(object sender, EventArgs e)
        {
            this.btnPause.Enabled = true;
            this.btnContince.Enabled = false;
            this.btnRedo.Enabled = false;
            this.btnCancel.Enabled = false;
            worker.ContinceAsync();
        }

        private void btnRedo_Click(object sender, EventArgs e)
        {
            this.btnPause.Enabled = true;
            this.btnContince.Enabled = false;
            this.btnRedo.Enabled = false;
            this.btnCancel.Enabled = false;
            worker.RedoAsync();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.btnStart.Enabled = true;
            this.btnContince.Enabled = false;
            this.btnRedo.Enabled = true;
            this.btnCancel.Enabled = false;
            worker.CancelAsync();
        }

        private void btnDeleteTgtFile_Click(object sender, EventArgs e)
        {
            try
            {
                File.Delete(this.txtTo.Text.Trim());
                lblMsg.Text = "已删除";
            }
            catch (Exception ex)
            {
                lblMsg.Text = ex.Message;
            }
        }
    }
}
